
package com.ttomato.game.glbasics;

import javax.microedition.khronos.opengles.GL10;

import com.ttomato.game.framework.Game;
import com.ttomato.game.framework.Screen;
import com.ttomato.game.framework.gl.FPSCounter;
import com.ttomato.game.framework.gl.Texture;
import com.ttomato.game.framework.gl.Vertices;
import com.ttomato.game.framework.impl.GLGame;
import com.ttomato.game.framework.impl.GLGraphics;

public class BobTest extends GLGame {

    @Override
    public Screen getStartScreen() {
        // TODO Auto-generated method stub
        return new BobScreen(this);
    }

    class BobScreen extends Screen {

        static final int NUM_BOBS = 1000;

        GLGraphics mGlGraphics;

        Texture mBobTexture;

        Vertices mBobModel;

        Bob[] mBobs;

        FPSCounter mFpsCounter;

        public BobScreen(Game game) {
            super(game);

            GLGame glGame = (GLGame) game;

            mGlGraphics = glGame.getGLGraphics();

            mBobTexture = new Texture(glGame, "newbobrgb888.png");

            mBobModel = new Vertices(mGlGraphics, 4, 12, false, true);
            mBobModel.setVertices(new float[] {
                    -16, -16, 0, 1,
                    16, -16, 1, 1,
                    16, 16, 1, 0,
                    -16, 16, 0, 0,
            }, 0, 16);
            mBobModel.setIndices(new short[] {
                    0, 1, 2, 2, 3, 0
            }, 0, 6);

            mBobs = new Bob[NUM_BOBS];
            for (int i = 0; i < NUM_BOBS; i++) {
                mBobs[i] = new Bob();
            }

            mFpsCounter = new FPSCounter();
        }

        @Override
        public void update(float deltaTime) {
            mGame.getInput().getTouchEvents();
            mGame.getInput().getKeyEvents();

            for (int i = 0; i < NUM_BOBS; i++) {
                mBobs[i].update(deltaTime);
            }
        }

        @Override
        public void present(float deltaTime) {
            GL10 gl = mGlGraphics.getGl();
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

            gl.glMatrixMode(GL10.GL_MODELVIEW);

            mBobModel.bind();
            for (int i = 0; i < NUM_BOBS; i++) {
                gl.glLoadIdentity();
                gl.glTranslatef(mBobs[i].mX, mBobs[i].mY, 0);
                mBobModel.draw(GL10.GL_TRIANGLES, 0, 6);
            }
            mBobModel.unbind();

            mFpsCounter.logFrame();
        }

        @Override
        public void pause() {

        }

        @Override
        public void resume() {
            GL10 gl = mGlGraphics.getGl();
            gl.glViewport(0, 0, mGlGraphics.getWidth(), mGlGraphics.getHeight());
            gl.glClearColor(1, 0, 0, 1);
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glOrthof(0, 320, 0, 480, -1, 1);

            mBobTexture.reload();
            gl.glEnable(GL10.GL_TEXTURE_2D);
            mBobTexture.bind();
        }

        @Override
        public void dispose() {

        }
    }

}
